<template>
  <el-container>
    <el-header style="height:80px;">
      <el-form
        ref="form"
        label-width="80px"
        style="margin-top:20px;"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item
              label="备份名称："
              prop="region"
            >
              <el-input
                v-model="search.cBackupFile"
                maxlength="30"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作时间">
              <el-col :span="11">
                <el-date-picker
                  v-model="startDate"
                  type="date"
                  placeholder="开始时间"
                  :editable="false"
                >
                </el-date-picker>
              </el-col>
              <el-col
                class="line"
                :span="2"
                style="text-align: center"
              >-</el-col>
              <el-col :span="11">
                <el-date-picker
                  v-model="endDate"
                  type="date"
                  placeholder="结束时间"
                  :editable="false"
                >
                </el-date-picker>
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button
              @click="query(search)"
              style="margin-left:10px;"
            >查询</el-button>
            <el-button
              @click="backup()"
              type="primary"
            >添加新备份</el-button>
          </el-col>
        </el-row>
      </el-form>

    </el-header>
    <el-main>
      <section class="contant_head">
        <span>图表
          <!-- <sub>Form</sub> -->
        </span>
      </section>
      <el-table
        :data="tableData"
        border
        style="width: 98%;margin:10px;text-align:center;"
        :header-cell-style="getRowClass"
      >
        <el-table-column
          prop="index"
          label="编号"
          height="50px"
          width="90"
        > </el-table-column>
        <el-table-column
          prop="cBackupFile"
          label="备份名称"
          width="350"
        ></el-table-column>
        <el-table-column
          prop="cBackupTime"
          label="创建时间"
        ></el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="160"
        >
          <template slot-scope="scope">
            <el-button
              class="edit-btn"
              @click="handleRestore(scope.$index, scope.row)"
              size="medium"
            >还原</el-button>
            <el-button
              class="delete-btn"
              @click="handleDelete(scope.$index, scope.row)"
              type="danger"
              size="medium"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10]"
          :page-size="limit"
          layout="total, sizes, prev, pager, next"
          :total="total"
          style="float:right"
        >
        </el-pagination>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import { api } from "@/fetch/api";


export default {
  data() {
    return {
      search: {
        cBackupFile: "",
      },
      currentPage: 1,
      limit: 10,
      total: null,
      startDate: null,
      endDate: null,
      tableData: [],
      sysBackup: {
        cBackupFile: "",
        cBackupId: 0,
        cBackupTime: "",
        cUserId: 0,
        endDate: "",
        remark: "",
        startDate: "",
        state: 0
      }
    };
  },
  methods: {
    getSystem(limit, pageIndex) {
      let search = {};
      search.endDate = this.endDate
        ? new Date(this.endDate).format("yyyy-MM-dd")
        : null;
      search.startDate = this.startDate
        ? new Date(this.startDate).format("yyyy-MM-dd")
        : null;
      api
        .systemPage(search, { pageSize: limit, currPage: pageIndex })
        .then(response => {
          if (response.status === 100) {
            this.tableData = response.result.list;
            this.tableData.forEach(function (item, index) {
              item.index = index + 1 + (pageIndex - 1) * limit;
            });
            this.total = response.result.totalCount;
          } else {
            this.$message.error(response.message);
          }
        });
    },
    query(search) {
      search.endDate = this.endDate
        ? new Date(this.endDate).format("yyyy-MM-dd")
        : null;
      search.startDate = this.startDate
        ? new Date(this.startDate).format("yyyy-MM-dd")
        : null;
      api.systemPage(search, { pageSize: 10, currPage: 1 }).then(response => {
        if (response.status === 100) {
          this.tableData = response.result.list;
          this.currentPage = 1;
          this.tableData.forEach(function (item, index) {
            item.index = index + 1;
          });
          this.total = response.result.totalCount;
        } else {
        }
      });
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getSystem(this.limit, val);
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getSystem(val, this.currentPage);
    },
    handleRestore(index, row) {
      this.$confirm("是否继续还原", "", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          api
            .systemRestore({
              cBackupId: row.cBackupId
            })
            .then(response => {
              if (response.status === 100) {
                this.$message.success("还原成功");
                this.getSystem(this.limit, this.currentPage);
              } else {
                this.$message.error(response.message);
              }
            });
        })
        .catch(() => { });
    },
    handleDelete(index, row) {
      this.$confirm("是否继续删除", "", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          api.systemRemove(row.cBackupId).then(response => {
            if (response.status === 100) {
              this.$message.success(response.message);
              this.getSystem(this.limit, this.currentPage);
            } else {
              this.$message.error(response.message);
            }
          });
        })
        .catch(() => { });
    },
    backup() {
      api.systemBackup().then(response => {
        if (response.status === 100) {
          this.$message.success("添加新备份成功");
          this.getSystem(this.limit, this.currentPage);
        } else {
          this.$message.error(response.message);
        }
      });
    },
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex == 0) {
        return "background:rgba(242,242,242,1)";
      } else {
        return "";
      }
    }
  },
  mounted() {
    this.getSystem(10, this.currentPage);
  }
};
</script>

<style lang="less" scoped>
@import url("../../assets/css/index.css");
.el-header {
  width: 98%;
  background: #fff;
  margin: 0 auto;
  margin-top: 20px;
  margin-bottom: 20px;
  .el-input,
  .el-select,
  .el-date-picker {
    width: 100%;
  }
}
.el-main {
  width: 98%;
  background: #fff;
  margin: 0 auto;
  padding: 0px;
  .contant_head {
    border-left: 3px solid #f7585d;
    background: #f9f9f9;
    height: 40px;
    span {
      line-height: 40px;
      font-size: 20px;
      padding-left: 10px;
      sub {
        font-size: 12px;
      }
    }
  }
}
.el-checkbox {
  margin-right: 12%;
  font-size: 20px;
  color: #4a4a4a;
}
</style>
<style>
.cell {
  text-align: center;
}
</style>


